package com.ljl;

import com.alibaba.fastjson.JSONObject;
import com.ljl.domain.XmlData;
import com.ljl.util.RedisTool;
import com.ljl.util.XmlResolveTool;
import io.netty.util.internal.StringUtil;
import org.redisson.Redisson;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;

import java.util.*;

/**
 * @author ljl
 * @version 1.0
 * @date 2022/10/9 13:27
 */
public class Main {

    public static void main(String[] args) {
        //xml文件路径
        String xmlFile = "src\\main\\resources\\conf-io.xml";
        XmlData xmlData = XmlResolveTool.resolveXml(xmlFile);
        System.out.println(xmlData);
        //保存到redis db：1
        //saveToRedis(xmlData, "127.0.0.1", 6379, "", 1);

        //处理数据，根据标签名将数据分类
        List<XmlData> list = new ArrayList<>();
        XmlResolveTool.collectionBottomXmlData(xmlData, list);
        Map<String, List<XmlData>> stringListMap = XmlResolveTool.collectionXmlDataByName(list);
        //需要保存的数据
        Map<String, Integer> integerDb = new HashMap<>();
        Map<String, Double> doubleDb = new HashMap<>();
        Map<String, String> stringDb = new HashMap<>();
        for (Map.Entry<String, List<XmlData>> entry : stringListMap.entrySet()) {
            //只处理ITEM标签
            if (entry.getKey().contains("ITEM")) {
                List<XmlData> xmlDataList = entry.getValue();
                for (XmlData data : xmlDataList) {
                    //获取标签的属性
                    Map<String, String> properties = data.getProperties();
                    if (properties != null) {
                        //属性名
                        String name = properties.get("name");
                        //属性类型
                        String type = properties.get("dt");
                        //属性值
                        String value = properties.get("value");
                        //属性名称为空则跳过
                        if (StringUtil.isNullOrEmpty(name)) {
                            continue;
                        }
                        if ("integer".equalsIgnoreCase(type)) {
                            integerDb.put(name, StringUtil.isNullOrEmpty(value) ? 0 : Integer.parseInt(value));
                        }else if ("double".equalsIgnoreCase(type)) {
                            doubleDb.put(name, StringUtil.isNullOrEmpty(value) ? (double) 0 : Double.parseDouble(value));
                        }else {
                            stringDb.put(name, StringUtil.isNullOrEmpty(value) ? "null" : value);
                        }
                    }
                }
            }
        }
        //开始将筛选过后的数据保存到redis
        RedisTool.saveAll("127.0.0.1", 6379, "", 2, integerDb);
        RedisTool.saveAll("127.0.0.1", 6379, "", 3, doubleDb);
        RedisTool.saveAll("127.0.0.1", 6379, "", 4, stringDb);
    }

    /**
     * 保存到redis
     * @param xmlData /
     * @param host redis主机名
     * @param port 端口号
     * @param password redis密码
     * @param database 数据库
     */
    public static void saveToRedis(XmlData xmlData, String host, int port, String password, int database) {
        Config config = new Config();
        config.setCodec(StringCodec.INSTANCE);
        config.useSingleServer()
                .setDatabase(database)
                .setPassword(Objects.equals(password, "") ? null : password)
                .setAddress("redis://" + host + ":" + port);
        RedissonClient client = Redisson.create(config);
        saveXmlData(xmlData, client);
        client.shutdown();
    }

    /**
     * 递归保存xml数据
     * @param xmlData /
     * @param client /
     */
    private static void saveXmlData(XmlData xmlData, RedissonClient client) {
        Map<String, String> properties = xmlData.getProperties();
        if (properties.size() != 0) {
            //保存到redis
            RList<Object> list = client.getList(xmlData.getAncestorNames());
            list.add(JSONObject.toJSONString(xmlData));
        }
        List<XmlData> children = xmlData.getChildren();
        for (XmlData child : children) {
            saveXmlData(child, client);
        }
    }

}
